package com.wits.qso.web.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.web.Servlets;

import com.wits.platform.common.utils.PageUtil;
import com.wits.platform.web.controller.BaseController;
import com.wits.platform.web.mvc.PageParam;
import com.wits.qso.entity.MyRegisterCourse;
import com.wits.qso.service.AttachmentService;
import com.wits.qso.service.BtableDaoService;
import com.wits.qso.service.CListService;
import com.wits.qso.service.ClsService;
import com.wits.qso.service.CourseRegisterService;
import com.wits.qso.service.CourseService;
import com.wits.qso.service.CreContentService;
import com.wits.qso.service.MyRegisterCourseService;
import com.wits.qso.service.StudentService;
import com.wits.qso.service.TeacherService;

/**
 * 我的课程
 * @author admin
 *
 */
@Controller
@RequestMapping(value = "qso/myRegisterCourse")
public class MyRegisterCourseController extends BaseController {
	
	@Autowired
	private BtableDaoService btableDaoService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private ClsService clsService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private CreContentService creContentService;
	@Autowired
	private AttachmentService attachmentService;
	@Autowired
	private CListService cListService;
	/**
	 * 注入myRegisterCourseService
	 */
	@Autowired
	private MyRegisterCourseService myRegisterCourseService;
	@Autowired
	private CourseRegisterService courseRegisterService;
	@Autowired
	private TeacherService teacherService;

	
	@RequestMapping(value = "/toList")
	public String toList() {
		System.out.println("in toList fun ");
		return "qso/myRegisterCourse/list";
	}
	/**
	 * 加载我的课程列表页面
	 * 
	 * @param model
	 * @param page
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public String list(Model model, @ModelAttribute("page") PageParam page, HttpServletRequest req) {
		return listRegisterCourse(model,page,req,"isMyRegisterCourse");
	}
	/**
	 * 显示注册的课程列表
	 * @param model
	 * @param page
	 * @param req
	 * @param tag 标记
	 * @return
	 */
	private String listRegisterCourse(Model model, @ModelAttribute("page") PageParam page, HttpServletRequest req,String tag){
		Map<String, Object> searchParams = Servlets.getParametersStartingWith(
				req, "search_");
		/**
		 * 如果是学生端我的课程查询，增加条件，只查询当前学生用户所注册并被审批过的记录
		 */
		if ("isMyRegisterCourse".equals(tag)) {
			searchParams.put(SearchFilter.Operator.EQ + "_student.stuUserId",
					this.getCurrentUserId());
			searchParams.put(SearchFilter.Operator.EQ + "_crStatus", "CO");
		}
		Page<MyRegisterCourse> listPage = myRegisterCourseService.findPage(
				MyRegisterCourse.class, searchParams, page);
		return PageUtil.toAjaxPageResult(listPage.getTotalElements(),
				listPage.getContent());
	}
	
	/**
	 * 根据课程id查找课程下的课件资料
	 * @param model
	 * @param courseId
	 * @return
	 */
	@RequestMapping(value = "/view/detail")
	public String toView(Model model,@RequestParam String courseId) {
		if(courseId==null||"".equals(courseId)){
			throw new IllegalArgumentException("param is null");
		}
		String cListTree=cListService.getCListTree(courseId);
		//根据id查询列表
		model.addAttribute( "cListTree",cListTree);
		return "qso/clistView/list";
	}

}
